/**
 * 
 */
package com.hww.dianqu;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

/**
 * @author:Huangwenwei
 * @date:2014-6-4
 * @time:下午8:39:30
 */
public class Test2_printMidleeNum {

	/**
	 * 获取中位数
	 * 
	 * @param array
	 * @return float
	 */
	public float getMidleNum(List<Float> array) {
		// 数组长度为偶数
		if (array.size() % 2 == 0 && array.size() != 0) {
			int theFirstEle = array.size() / 2 - 1;
			int theSecondEle = array.size() / 2;
			return (array.get(theFirstEle) + array.get(theSecondEle)) / 2;
		}
		// 数组长度为奇数
		else if (array.size() != 0 && array.size() % 2 == 1) {
			int ele = (array.size() - 1) / 2;
			return array.get(ele);
		} else
			return 0;
	}

	/**
	 * 随机生成一个数组的长度，根据随机生成的数组长度随机生长一个数组
	 * 
	 * @return float[]
	 */
	public static List<Float> randomAyyary() {
		Random randomLength = new Random();
		Random randomArray = new Random();
		// 随机生成一个数组长度
		int arrayLength = randomLength.nextInt(15);
		System.out.println("随机生成的数组长度为：" + arrayLength);
		List<Float> target = new ArrayList<Float>();
		for (int index = 0; index < arrayLength; index++) {
			float temp = randomArray.nextInt(100);
			target.add(temp);
		}
		Iterator<Float> iterArray = target.iterator();
		System.out.print("随机生成的数组为：");
		while (iterArray.hasNext()) {
			float ele = (float) iterArray.next();
			System.out.print("  " + ele);
		}
		System.out.println();
		return target;
	}

	/**
	 * @param args
	 *            void
	 */
	public static void main(String[] args) {
		Test2_printMidleeNum demo = new Test2_printMidleeNum();
		System.out.println("计算出的中位数为："
				+ demo.getMidleNum(Test2_printMidleeNum.randomAyyary()));
	}

}
